package com.tianqu.tms.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dao.CarDao;
import com.tianqu.tms.dao.CodesDao;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dto.CodesDto;
import com.tianqu.tms.core.base.TmsEntity;

/**
 * 字典表
 * 
 * @author tianshifu
 *
 */
public class Codes extends TmsEntity {

	/**
	 * Serializable id
	 */
	private static final long serialVersionUID = 8941684905894069063L;

	private long ID;// 主键ID

	private int itemId;// 数据类别

	private int itemCode;// 缺陷描述

	private String itemValue;// 缺陷代码

	/******************* 定义获取code信息类型 *************************/

	public final static int CAR_OWNER = 101;// 车主/买主

	public final static int VEHICLE_COUNTRY = 102;// 车辆国别

	public final static int VEHICLE_TYPE = 103;// 车辆类别

	public final static int USER_NATURE = 104;// 使用性质

	public final static int VEHICLE_COLOR = 105;// 车辆颜色

	public final static int ATTACHMENT_PROPERTY = 106;// 附件属性

	public final static int FUEL_TYPE = 107;// 燃料种类

	public final static int GEARBOX_TYPE = 108;// 变速箱类型

	public final static int PLATE_TYPE = 109;// 号牌种类

	public final static int USER_TYPE = 110;// 会员类型

	public final static int AUCTION_TIME = 111;// 竞价场次时间

	public final static int AUCTION_TYPR = 112;// 竞价场次类型

	public final static int PROXYCAR_STATUS = 113;// 待办车辆状态

	public final static int GAIN_TYPE = 114;// 获得方式

	public final static int TRANSFER_TYPE = 115;// 申请事项

	public final static int ACCEPT_STATUS = 116;// 过户状态

	public final static int HIS_STATUS = 117;// 历史表状态

	public final static int TRANSMARKET = 118;// 交易市场

	public final static int SEARCH_ACCEPT_TYPE = 118;// 查询状态

	public final static int SOURCE_TYPE = 119;// 车辆来源

	public final static int WORK_TYPE = 120;// 车辆来源

	public final static int RENTAL_TYPE = 121;// 租位类型

	public final static int GATEWAY_TYPE = 122;// 匝道编号

	public final static int LICENSECOLOR_TYPE = 123;// 车身颜色

	public final static int THREADSTATUS_TYPE = 128;// 进出状态

	public final static int THREADTYPE_TYPE = 129;// 识别方式

	public final static int REGISTERSTATUS_TYPE = 130;// 库存状态

	public final static int BARCODENUM_TYPE = 131;// 挂起状态

	public final static int ISSHELVE_TYPE = 132;// 是否上架

	public final static int ISMATCH_TYPE = 133;// 车款是否匹对

	public final static int HANDLETYPE_TYPE = 134;// 操作类型

	public final static int ISPUSH_TYPE = 135;// 是否推送

	public final static int RECOMMEND_TYPE = 136;// 车源推荐权重

	public final static int LICENSEPLATE_TYPE = 137;// 号牌种类

	public final static int LICENSEPLATE_COLOR = 138;// 号牌颜色

	public final static int MEMBERUSER_TYPE = 139;// 用户类型

	public final static int VALIDSTATUS = 140; // 有效状态

	public final static int GATEWAY_IP = 141; // 道闸IP

	public final static int TROUBLE_TYPE = 143; // 问题类型

	public final static int CHARGE_TYPE_CHARGE = 142; // 收费类型
	public final static int CHARGE_TYPE_REFUND = 151; // 退费操作

	public final static int INTERNAL_SCORE = 144; // 车辆内饰评测

	public final static int EXTERNAL_SCORE = 145; // 车辆外观评测

	public final static int CONTRACT_UPDATE_TYPE = 146; // 合同操作记录类型

	public final static int RENTAL_AREA = 147; // 租位区域

	public final static int CONTRACT_STATUS = 148; // 合同状态

	public final static int RENTAL_UPDATE_TYPE = 149; // 租位操作记录类型

	public final static int CHANGE_BODY_TYPE = 152;//
	/**
	 * 实例化codes表
	 * 
	 * @return
	 */
	private static Codes instance;

	List<Codes> codes = null;

	/**
	 * 加载Codes
	 * 
	 * @return
	 * @throws TmsDaoException
	 */
	public static Codes getInstance() throws TmsDaoException {
		if (null == instance) {
			instance = new Codes();
			instance.init();
		}
		return instance;
	}

	/**
	 * 实例化Code
	 * 
	 * @throws TmsDaoException
	 */
	public void init() throws TmsDaoException {
		codes = new ArrayList<Codes>();
		setCodes();
	}

	public List<CodesDto> getCodes(int itemId, String itemValue) throws TmsDaoException {
		CodesDao dao = (CodesDao) TmsDao.getInstance(TmsDao.TMS_CODES);
		return dao.getCodes(itemId, itemValue);
	}

	/**
	 * 获取Codes信息存在静态codes中
	 * 
	 * @throws TmsDaoException
	 */
	public void setCodes() throws TmsDaoException {
		CodesDao dao = (CodesDao) TmsDao.getInstance(TmsDao.TMS_CODES);
		List<CodesDto> list = dao.instanceCodes();
		for (int i = 0; i < list.size(); i++) {
			CodesDto dto = (CodesDto) list.get(i);
			Codes entity = new Codes();
			entity.setItemId(dto.getItemId());
			entity.setItemCode(dto.getItemCode());
			entity.setItemValue(dto.getItemValue());

			codes.add(entity);
		}
	}

	/**
	 * 根据itemId获取code信息
	 * 
	 * @param itemId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List getCodeslistByItemId(int itemId) {
		Map map = null;// 用于存储itemCode和itemValue
		List list = new ArrayList();

		for (int i = 0; i < codes.size(); i++) {
			Codes dto = (Codes) codes.get(i);
			if (dto.getItemId() == itemId) {
				map = new HashMap();
				map.put("itemCode", dto.getItemCode());
				map.put("itemValue", dto.getItemValue());
				list.add(map);
			}
		}
		if (list.size() == 0) {
			return null;
		}

		return list;
	}

	/**
	 * 通过itemId获得item对象列表
	 * 
	 * @param itemId
	 * @return
	 */
	public Map<Integer, String> getCodesMapByItemId(int itemId) {
		Map<Integer, String> map = new HashMap<Integer, String>();
		for (int i = 0; i < codes.size(); i++) {
			Codes dto = (Codes) codes.get(i);
			if (dto.getItemId() == itemId) {
				map.put(dto.getItemCode(), dto.getItemValue());
			}
		}
		return map;
	}
	
	public List<Codes> getCodesListByItemId(int itemId) {
		List<Codes> list = new ArrayList<Codes>();
		for (int i = 0; i < codes.size(); i++) {
			Codes dto = (Codes) codes.get(i);
			if (dto.getItemId() == itemId) {
				list.add(dto);
			}
		}
		return list;
	}

	/**
	 * 通过itemId获得item对象列表,将时间装换成 yyyy-MM-dd hh:mm
	 * 
	 * @param itemId
	 * @return
	 */
	public Map<Integer, String> getCodesMapByItemIdTime(int itemId) {
		Map<Integer, String> map = new HashMap<Integer, String>();
		for (int i = 0; i < codes.size(); i++) {
			Codes dto = (Codes) codes.get(i);
			if (dto.getItemId() == itemId) {
				StringBuffer sb = new StringBuffer();
				sb.append(DateUtil.DateToString11(new Date()));
				sb.append(" ");
				sb.append(dto.getItemValue());
				map.put(dto.getItemCode(), sb.toString());
			}
		}
		return map;
	}

	/**
	 * 根据ItemId和ItemCode获取具体itemValue值
	 * 
	 * @param itemId
	 * @param itemCode
	 * @return
	 */
	public String getItemValueByItemIdAndItemCode(int itemId, int itemCode) {
		String str = TmsConstants.BLANK;
		for (int i = 0; i < codes.size(); i++) {
			Codes entity = (Codes) codes.get(i);
			if (entity.getItemId() == itemId && entity.getItemCode() == itemCode) {
				return entity.getItemValue();
			}
		}
		return str;
	}

	/**
	 * 根据itemId和itemValue获取itemCode
	 * 
	 * @param itemId
	 * @param itemValue
	 * @return
	 */
	public int getItemCodeByItemIdAndItemValue(int itemId, String itemValue) {
		int code = 0;
		for (int i = 0; i < codes.size(); i++) {
			Codes entity = (Codes) codes.get(i);
			if (entity.getItemId() == itemId && itemValue.equals(entity.getItemValue())) {
				return entity.getItemCode();
			}
		}
		return code;
	}

	public long getID() {
		return ID;
	}

	public void setID(long iD) {
		ID = iD;
	}

	public int getItemId() {
		return itemId;
	}

	public void setItemId(int itemId) {
		this.itemId = itemId;
	}

	public int getItemCode() {
		return itemCode;
	}

	public void setItemCode(int itemCode) {
		this.itemCode = itemCode;
	}

	public String getItemValue() {
		return itemValue;
	}

	public void setItemValue(String itemValue) {
		this.itemValue = itemValue;
	}

	@Override
	public void save() throws TmsDaoException, TmsException {
		// TODO Auto-generated method stub

	}

}
